﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WF.DAL;
using WF.Model;

namespace WF.BusinessObject
{
    public class MenuItem : BOBase<MenuItemEntity, DAL.MenuItem>
    {
        #region 构造函数

        public MenuItem() : base() { }
        public MenuItem(WFDBDataContext DataContext)
            : base(DataContext)
        {

        }
        #endregion

        #region 实现基类abstract方法
        /// <summary>
        /// 实现基类的GetById abstract方法
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public override MenuItemEntity GetById(string ID)
        {
            var obj = DataContext.MenuItems.SingleOrDefault(o => o.RowID == ID);
            return new MenuItemEntity(obj);
        }
        #endregion

        public List<MenuItemEntity> GetSubMenuItems(MenuItemEntity parentMenuItem)
        {
            List<MenuItemEntity> rt = new List<MenuItemEntity>();
            if (null == parentMenuItem)
            {
                var objs = from a in DataContext.MenuItems where (null == a.ParentID || string.Empty == a.ParentID) && (a.FlagDeleted == (short)Common.FlagDeleted.活动) orderby a.MenuOrder select a;
                foreach (DAL.MenuItem o in objs)
                {
                    rt.Add(new MenuItemEntity(o));
                }
            }
            else
            {
                var objs = from a in DataContext.MenuItems where (a.ParentID == parentMenuItem.ID) && (a.FlagDeleted == (short)Common.FlagDeleted.活动) orderby a.MenuOrder select a;
                foreach (DAL.MenuItem o in objs)
                {
                    rt.Add(new MenuItemEntity(o));
                }
            }
            return rt;

        }
        public List<MenuItemEntity> GetSubMenuItems()
        {
            return GetSubMenuItems(null);
        }
    }
}
